/**
 * 
 */
package me.pearjelly.filters.commons;

import java.util.regex.Pattern;

import me.pearjelly.filters.VisitFilter;

import edu.uci.ics.crawler4j.url.WebURL;

/**
 * @author 韩孝冰
 * 
 */
public class UrlFilter implements VisitFilter {
	private VisitFilter visitFilter;
	private Pattern[] filters;

	public UrlFilter(String regString, VisitFilter visitFilter) {
		this.visitFilter = visitFilter;
		this.filters = new Pattern[] { Pattern.compile(regString) };
	}

	public UrlFilter(String[] regStrings, VisitFilter visitFilter) {
		this.visitFilter = visitFilter;
		this.filters = new Pattern[regStrings.length];
		int i = 0;
		for (String string : regStrings) {
			this.filters[i++] = Pattern.compile(string);
		}
	}

	@Override
	public boolean filter(WebURL webURL) {
		String url = webURL.getURL().toLowerCase();
		for (Pattern filter : filters) {
			if (filter.matcher(url).matches()) {
				return false;
			}
		}
		return null == visitFilter || visitFilter.filter(webURL);
	}
}
